package web.directories.action;

import com.opensymphony.xwork2.ActionSupport;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 26/09/12
 * Time: 11:03
 * To change this template use File | Settings | File Templates.
 */

@Namespace("/Directories/Cda")
public class CdaController extends ActionSupport {

    private ProtocolCRUDService protocolCRUDService;
    private DrogaService drogaService;
    private CentroCRUDService centroService;
    private InvestigatorCRUDService investigatorService;
    private CdaService cdaService;
    private CdaMolecula cdaMolecula = new CdaMolecula();
    private Cdamaster cdamaster = new Cdamaster();
    private List<Droga> drogaList = new ArrayList<Droga>();
    private List<Centro> centroList = new ArrayList<Centro>();
    private String category;
    private Integer hdhinvestigatorid;
    private List<CdaCriteria> list = new ArrayList<CdaCriteria>();
    private Integer id;
    private String investigator;
    private Integer clear;
    private Integer centroId;


    @Autowired
    public CdaController(ProtocolCRUDService protocolCRUDService, DrogaService drogaService,
                         CentroCRUDService centroService, InvestigatorCRUDService investigatorService,
                         CdaService cdaService) {
        this.protocolCRUDService = protocolCRUDService;
        this.drogaService = drogaService;
        this.centroService = centroService;
        this.investigatorService = investigatorService;
        this.cdaService = cdaService;
    }

    @Action(value="main", results={
            @Result(name="success",location="/directories.cda.tiles", type="tiles")
    })
    public String execute() {

       centroList = centroService.findAllSites();
       drogaList = drogaService.getDrogas();
       return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success", location="/directories.cda.tiles", type="tiles")
    })
    public String list() {



        if( category != null && category.equals("")) {

           if( cdaMolecula.getDroga().getId() != null && cdaMolecula.getDroga().getId() != null  ){
                ArrayList<CdaCriteria> listMolecula = (ArrayList<CdaCriteria>)cdaService.listMolecula(cdaMolecula);
                list.addAll(listMolecula);
           }
            if( cdamaster.getCentro() != null && cdamaster.getCentro().getId() != null    ){
                ArrayList<CdaCriteria> listMaster = (ArrayList<CdaCriteria>)cdaService.listMaster(cdamaster);
                list.addAll(listMaster) ;
            }
            if( cdaMolecula.getDroga().getId() == null &&  cdamaster.getCentro().getId() == null   ){
                ArrayList<CdaCriteria> listMolecula = (ArrayList<CdaCriteria>)cdaService.listMolecula(cdaMolecula);
                list.addAll(listMolecula);
                ArrayList<CdaCriteria> listMaster = (ArrayList<CdaCriteria>)cdaService.listMaster(cdamaster);
                list.addAll(listMaster) ;
            }

        }
        if( category != null && category.equals("Molecula") && cdamaster.getCentro().getId() == null ) {
            ArrayList<CdaCriteria> listMolecula = (ArrayList<CdaCriteria>)cdaService.listMolecula(cdaMolecula);
            list.addAll(listMolecula);
        }
        if( category != null && category.equals("Master") && cdaMolecula.getDroga().getId() == null  ) {

            ArrayList<CdaCriteria> listMaster = (ArrayList<CdaCriteria>)cdaService.listMaster(cdamaster);
            list.addAll(listMaster) ;
        }

        centroList = centroService.findAllSites();
        drogaList = drogaService.getDrogas();

        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/directories.cda.create.tiles", type="tiles")
    })
    public String createRole() {

        if( clear == null){
        if( category != null && id != null && category.equals("Molecula")){
            cdaMolecula = cdaService.findCdaMoleculaById(id);
            investigator =  cdaMolecula.getInvolucrado().getApellido() + ", " + cdaMolecula.getInvolucrado().getNombre();
            hdhinvestigatorid = cdaMolecula.getInvolucrado().getId();
        }
        if( category != null && id != null && category.equals("Master")){
            cdamaster = cdaService.findCdaMasterById(id);
        }
        }else
        {
            cdamaster = new Cdamaster();
            cdaMolecula = new CdaMolecula();
        }

        centroList = centroService.findAllSites();
        drogaList = drogaService.getDrogas();

        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String save() {

            if( category.equals("Molecula")){
                cdaMolecula.setDroga(drogaService.findById(cdaMolecula.getDroga().getId()));
                cdaMolecula.setInvolucrado(investigatorService.getById(hdhinvestigatorid));

                try {
                    cdaService.saveMolecula(cdaMolecula);
                } catch (Exception e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

            }else{
                cdamaster.setCentro( (Centro)centroService.findById(cdamaster.getCentro().getId()) );
                cdaService.saveMaster(cdamaster);
            }



        return SUCCESS;
    }



    public CdaMolecula getCdaMolecula() {
        return cdaMolecula;
    }

    public void setCdaMolecula(CdaMolecula cdaMolecula) {
        this.cdaMolecula = cdaMolecula;
    }

    public Cdamaster getCdamaster() {
        return cdamaster;
    }

    public void setCdamaster(Cdamaster cdamaster) {
        this.cdamaster = cdamaster;
    }

    public List<Droga> getDrogaList() {
        return drogaList;
    }

    public void setDrogaList(List<Droga> drogaList) {
        this.drogaList = drogaList;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public List<Centro> getCentroList() {
        return centroList;
    }

    public void setCentroList(List<Centro> centroList) {
        this.centroList = centroList;
    }

    public Integer getHdhinvestigatorid() {
        return hdhinvestigatorid;
    }

    public void setHdhinvestigatorid(Integer hdhinvestigatorid) {
        this.hdhinvestigatorid = hdhinvestigatorid;
    }

    public List<CdaCriteria> getList() {
        return list;
    }

    public void setList(List<CdaCriteria> list) {
        this.list = list;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getClear() {
        return clear;
    }

    public void setClear(Integer clear) {
        this.clear = clear;
    }

    public Integer getCentroId() {
        return centroId;
    }

    public void setCentroId(Integer centroId) {
        this.centroId = centroId;
    }
}
